//
//  RMBlurryView.m
//  RMNote
//
//  Created by lxf on 16/4/26.
//  Copyright © 2016年 lxf. All rights reserved.
//

#import "RMBlurryView.h"



@interface RMBlurryView()

// 图片的集合
@property(nonatomic,strong)NSArray *imageArray;


@end


@implementation RMBlurryView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.blurryImageView = [[UIImageView alloc]initWithFrame:self.bounds];
        [self addSubview:self.blurryImageView];
    }
    return self;
}



- (void)setImage:(UIImage *)image{
    _image = image;
    self.blurryImageView.frame = self.bounds;
    NSLog(@"%f",self.bounds.size.width);
    self.blurryImageView.image = _image;

}

/*
 4. 循环取出 两个触摸对象,分别设置她的显示图片
 4.1 实现 两星星 相同
 4.2 不同: 创建一个 图片集合
 4.3 分别设置
 */

//懒加载图片集合
- (NSArray *)imageArray
{
    if (!_imageArray) {
        
        _imageArray = @[
                        [UIImage imageNamed:@"spark_blue"],
                        [UIImage imageNamed:@"spark_magenta"]
                        
                        
                        ];
    }
    
    return _imageArray;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //    3. 有触摸事件,在触摸事件中添加 UIImageView 实现 有小星星的功能
    
    NSInteger i = 0;
    
    for (UITouch *touch in touches) {
        
        //2. 获取一下当前的位置 : 你图片将要显示的位置
        CGPoint loc = [touch locationInView:touch.view];
        
        //3. 创建UIIMageView
        UIImageView *starImageVIew = [[UIImageView alloc]initWithImage:self.imageArray[i]];
        
        
        //4. 设置 UIIMageVIew的显示位置
        starImageVIew.center = loc;
        
        //5. 添加
        [self addSubview:starImageVIew];
        
        //6. 角标累加
        i++;
        
        
        //6.内存问题:动画
        [UIView animateWithDuration:1.0 animations:^{
            
            
            //修改透明度
            starImageVIew.alpha = 0.1;
            
        } completion:^(BOOL finished) {
            
            
            //直接移除子控件
            [UIView animateWithDuration:1.0 animations:^{
                
                [starImageVIew removeFromSuperview];
            }];
            
        }];
        
    }
    
    
    
    
    
    
    
    
}
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //    3. 有触摸事件,在触摸事件中添加 UIImageView 实现 有小星星的功能
    
    NSInteger i = 0;
    
    for (UITouch *touch in touches) {
        
        //2. 获取一下当前的位置 : 你图片将要显示的位置
        CGPoint loc = [touch locationInView:touch.view];
        
        //3. 创建UIIMageView
        UIImageView *starImageVIew = [[UIImageView alloc]initWithImage:self.imageArray[i]];
        
        
        //4. 设置 UIIMageVIew的显示位置
        starImageVIew.center = loc;
        
        //5. 添加
        [self addSubview:starImageVIew];
        
        //6. 角标累加
        i++;
        
        //6.内存问题:动画
        [UIView animateWithDuration:1.0 animations:^{
            
            
            //修改透明度
            starImageVIew.alpha = 0.1;
            
        } completion:^(BOOL finished) {
            
            
            //直接移除子控件
            [UIView animateWithDuration:1.0 animations:^{
                
                [starImageVIew removeFromSuperview];
            }];
            
        }];
        
        
    }
    
    
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    
}


- (void)test
{
    //    //    3. 有触摸事件,在触摸事件中添加 UIImageView 实现 有小星星的功能
    //
    //    //1. 获取一下触摸的对象
    //    UITouch *touch = touches.anyObject;
    //
    //    //2. 获取一下当前的位置 : 你图片将要显示的位置
    //    CGPoint loc = [touch locationInView:touch.view];
    //
    //    //3. 创建UIIMageView
    //    UIImageView *starImageVIew = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"spark_blue"]];
    //
    //
    //    //4. 设置 UIIMageVIew的显示位置
    //    starImageVIew.center = loc;
    //
    //    //5. 添加
    //    [self addSubview:starImageVIew];
}






/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */




@end
